package com.android.signhelper.ui;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.signhelper.R;
import com.android.signhelper.bean.Student;
import com.android.signhelper.interfaces.OnClickListener;
import com.android.signhelper.util.MyApplication;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ikata on 2018/11/14.
 */

public class RvStudentAdapter extends RecyclerView.Adapter<RvStudentAdapter.MyViewHolder> {

    private List<Student> list;
    private OnClickListener onClickListener;

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public List<Student> getList() {
        return list;
    }

    public void addStudent(List<Student> list)
    {
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    public void update(Student student){
        for (Student student1 : list) {
            if (student1.equals(student)) {
                int position=list.indexOf(student1);
                list.set(position, student);
                notifyItemChanged(position);
                return;
            }
        }
        list.add(student);
        notifyItemInserted(getItemCount()-1);
    }

    public RvStudentAdapter(){
        list = new ArrayList<>();
    }

    /**
     * 匹配信息 更新msg
     * @param devUuidOrUserName devUuid userName 一个匹配成功即可
     * @param msg
     */
    public void showMsg(String devUuidOrUserName,String msg)
    {
        boolean flag = false;
        for (Student student : list) {
            //signInfo 可能为NULL
            if (student.getSignInfo()!=null&&student.getSignInfo().getDevUuid().equals(devUuidOrUserName)){
                flag = true;
            }
            if (student.getUserName().equals(devUuidOrUserName)){
                flag = true;
            }
            if (flag) {
                student.setMessage(msg);
                int position = list.indexOf(student);
                notifyItemChanged(position);
                notifyDataSetChanged();
            }
        }

    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_content, parent, false);
        return new MyViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        final Student student=list.get(position);
        holder.tvName.setText(student.getName());
        holder.tvUserName.setText(student.getUserName());
        holder.tvMsg.setText(student.getMessage());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickListener.setOnClickListener(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder{

        private TextView tvName;
        private TextView tvUserName;
        private TextView tvMsg;
        public MyViewHolder(View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_name);
            tvUserName = itemView.findViewById(R.id.tv_user_name);
            tvMsg = itemView.findViewById(R.id.tv_msg);
        }
    }
}
